/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bo;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.ipti.br.tag.dao.PerformanceFacade;
import org.ipti.br.tag.entity.Performance;
import org.ipti.br.tag.entity.enums.Reference;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.inject.Inject;
import javax.ejb.Stateless;
import org.ipti.br.tag.entity.Actor;
import org.w3c.dom.NameList;
import org.ipti.br.tag.util.ServiceUtil;

/**
 *
 * @author israel
 */
@Stateless
public class PerformanceBO {

    @Inject
    private PerformanceFacade performanceDAO;

    public PerformanceFacade getPerformanceDAO() {
        return performanceDAO;
    }
    
    public void create(Performance performance) {
        getPerformanceDAO().create(performance);
    }

    public void edit(Performance performance) {
        getPerformanceDAO().edit(performance);
    }

    public void remove(Performance performance) {
        getPerformanceDAO().remove(performance);
    }

    public Performance find(Object id) {
        return getPerformanceDAO().find(id);
    }

    public List<Performance> findAll() {
        return getPerformanceDAO().findAll();
    }

    public Integer gradesQuantityForUnityDiscipline(Long disciplineID,
            Reference reference, Long referenceID) {
        return getPerformanceDAO().gradesQuantityForUnityDiscipline(disciplineID, reference, referenceID);
    }

    public List<Performance> actorGradesPerReferenceDiscipline(Long actorID,
            Long disciplineID, Reference reference, Long referenceID) {
        return getPerformanceDAO().actorGradesPerReferenceDiscipline(actorID, disciplineID, reference, referenceID);
    }

    public List<Performance> modifiedPerfFreqBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        return getPerformanceDAO().modifiedPerfFreqBetweenDate(initialDate, finalDate, serialNumber);
    }

    public String export(List<String> namesList, 
            String unityName, List<String> disciplineList) {

        String file = ServiceUtil.getRealPath("/tmp/") + "/" + "Avaliação " + unityName + ".pdf";
        System.out.println("XXXXXXXXXXXXXXXXXXXX "+file);
        Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.NORMAL, BaseColor.BLACK);
        Font defaultFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.BLACK);
        Font columnHeader = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL, BaseColor.BLACK);

        try {
            Document document = new Document(PageSize.LETTER.rotate());
            PdfWriter.getInstance(document, new FileOutputStream(file));
            //document.setPageSize(PageSize.A4);
            document.setMargins(5, 5, 5, 5);
            document.setMarginMirroring(true);
            document.open();

            document.addAuthor("IPTI");
            document.addCreator("IPTI");

            for (String name : namesList) {
                Paragraph title = new Paragraph("RENDIMENTO ESCOLAR", titleFont);
                title.setAlignment(Element.ALIGN_CENTER);
                document.add(title);
                Paragraph space = new Paragraph();
                addEmptyLine(space, 1);
                document.add(space);

                document.add(new Paragraph("Ator: " + name, defaultFont));
                document.add(new Paragraph("Unidade: " + unityName, defaultFont));
                document.add(space);

                float maxNameSize = 0F;
                for (String disciplineName : disciplineList) {
                    float aux = ((((float) disciplineName.length()) / 5) + 5);
                    if (aux > maxNameSize) {
                        maxNameSize = aux;
                    }
                }

                float columns[] = new float[22]; //quantidade de nota fixa
                columns[0] = maxNameSize;
                for (int i = 1; i < columns.length; i++) {
                    columns[i] = 2.75F;
                }

                PdfPTable table = new PdfPTable(columns);
                table.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(" ");


                //==========específico
                PdfPCell cell1 = new PdfPCell(new Phrase("AB", columnHeader));
                cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell1);

                PdfPCell cell2 = new PdfPCell(new Phrase("VA", columnHeader));
                cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell2);

                PdfPCell cell3 = new PdfPCell(new Phrase("1ª Unid.", columnHeader));
                cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell3);

                PdfPCell cell4 = new PdfPCell(new Phrase("AB", columnHeader));
                cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell4);

                PdfPCell cell5 = new PdfPCell(new Phrase("VA", columnHeader));
                cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell5);

                PdfPCell cell6 = new PdfPCell(new Phrase("2ª Unid.", columnHeader));
                cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell6);

                PdfPCell cell7 = new PdfPCell(new Phrase("MS", columnHeader));
                cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell7);

                PdfPCell cell8 = new PdfPCell(new Phrase("RS1", columnHeader));
                cell8.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell8);

                PdfPCell cell9 = new PdfPCell(new Phrase("MGS", columnHeader));
                cell9.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell9);

                PdfPCell cell10 = new PdfPCell(new Phrase("AB", columnHeader));
                cell10.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell10);

                PdfPCell cell11 = new PdfPCell(new Phrase("VA", columnHeader));
                cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell11);

                PdfPCell cell12 = new PdfPCell(new Phrase("3ª Unid.", columnHeader));
                cell12.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell12);

                PdfPCell cell13 = new PdfPCell(new Phrase("AB", columnHeader));
                cell13.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell13);

                PdfPCell cell14 = new PdfPCell(new Phrase("VA", columnHeader));
                cell14.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell14);

                PdfPCell cell15 = new PdfPCell(new Phrase("4ª Unid.", columnHeader));
                cell15.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell15);

                PdfPCell cell16 = new PdfPCell(new Phrase("MS", columnHeader));
                cell16.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell16);

                PdfPCell cell17 = new PdfPCell(new Phrase("RS2", columnHeader));
                cell17.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell17);

                PdfPCell cell18 = new PdfPCell(new Phrase("MGS", columnHeader));
                cell18.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell18);

                PdfPCell cell19 = new PdfPCell(new Phrase("MA", columnHeader));
                cell19.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell19);

                PdfPCell cell20 = new PdfPCell(new Phrase("RF", columnHeader));
                cell20.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell20);

                PdfPCell cell21 = new PdfPCell(new Phrase("MF", columnHeader));
                cell21.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell21);
                //================

                for (String disciplineName : disciplineList) {
                    PdfPCell cell = new PdfPCell(new Phrase(disciplineName, columnHeader));
                    table.addCell(cell);
                    for (int i = 1; i <= 21; i++) { //quantidade de nota fixa
                        table.addCell(" ");
                    }
                }
                document.add(table);
                document.newPage();
            }
            document.close();
            return file;
        } catch (Exception ex) {
        }

        return "";
    }

    public String export2(List<String> namesList,
            String unityName, String disciplineName) {

        String file = "/tmp/Avaliação " + unityName + ".pdf";
        Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.NORMAL, BaseColor.BLACK);
        Font defaultFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.BLACK);
        Font columnHeader = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL, BaseColor.BLACK);

        try {
            Document document = new Document(PageSize.LETTER.rotate());
            PdfWriter.getInstance(document, new FileOutputStream(file));
            //document.setPageSize(PageSize.A4);
            document.setMargins(10, 10, 10, 10);
            document.setMarginMirroring(true);
            document.open();

            document.addAuthor("IPTI");
            document.addCreator("IPTI");

            Paragraph title = new Paragraph("RENDIMENTO ESCOLAR", titleFont);
            title.setAlignment(Element.ALIGN_CENTER);
            document.add(title);
            Paragraph space = new Paragraph();
            addEmptyLine(space, 1);
            document.add(space);

            document.add(new Paragraph("Unidade: " + unityName, defaultFont));
            document.add(new Paragraph("Disciplina: " + disciplineName, defaultFont));
            document.add(space);

            float maxNameSize = 0F;
            for (String name : namesList) {
                float aux = ((((float) name.length()) / 5) + 4);
                if (aux > maxNameSize) {
                    maxNameSize = aux;
                }
            }

            float columns[] = new float[22]; //quantidade de nota fixa
            columns[0] = maxNameSize;
            for (int i = 1; i < columns.length; i++) {
                columns[i] = 1.75F;
            }

            PdfPTable table = new PdfPTable(columns);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(" ");
            table.setWidthPercentage(95);

            //==========específico
            PdfPCell cell1 = new PdfPCell(new Phrase("AB", columnHeader));
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell1);

            PdfPCell cell2 = new PdfPCell(new Phrase("VA", columnHeader));
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell2);

            PdfPCell cell3 = new PdfPCell(new Phrase("1ª Unid", columnHeader));
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell3);

            PdfPCell cell4 = new PdfPCell(new Phrase("AB", columnHeader));
            cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell4);

            PdfPCell cell5 = new PdfPCell(new Phrase("VA", columnHeader));
            cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell5);

            PdfPCell cell6 = new PdfPCell(new Phrase("2ª Unid", columnHeader));
            cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell6);

            PdfPCell cell7 = new PdfPCell(new Phrase("MS", columnHeader));
            cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell7);

            PdfPCell cell8 = new PdfPCell(new Phrase("RS1", columnHeader));
            cell8.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell8);

            PdfPCell cell9 = new PdfPCell(new Phrase("MGS", columnHeader));
            cell9.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell9);

            PdfPCell cell10 = new PdfPCell(new Phrase("AB", columnHeader));
            cell10.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell10);

            PdfPCell cell11 = new PdfPCell(new Phrase("VA", columnHeader));
            cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell11);

            PdfPCell cell12 = new PdfPCell(new Phrase("3ª Unid", columnHeader));
            cell12.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell12);

            PdfPCell cell13 = new PdfPCell(new Phrase("AB", columnHeader));
            cell13.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell13);

            PdfPCell cell14 = new PdfPCell(new Phrase("VA", columnHeader));
            cell14.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell14);

            PdfPCell cell15 = new PdfPCell(new Phrase("4ª Unid", columnHeader));
            cell15.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell15);

            PdfPCell cell16 = new PdfPCell(new Phrase("MS", columnHeader));
            cell16.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell16);

            PdfPCell cell17 = new PdfPCell(new Phrase("RS2", columnHeader));
            cell17.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell17);

            PdfPCell cell18 = new PdfPCell(new Phrase("MGS", columnHeader));
            cell18.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell18);

            PdfPCell cell19 = new PdfPCell(new Phrase("MA", columnHeader));
            cell19.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell19);

            PdfPCell cell20 = new PdfPCell(new Phrase("RF", columnHeader));
            cell20.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell20);

            PdfPCell cell21 = new PdfPCell(new Phrase("MF", columnHeader));
            cell21.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell21);
            //================

            for (String name : namesList) {
                PdfPCell cell = new PdfPCell(new Phrase(name, columnHeader));
                table.addCell(cell);
                for (int i = 1; i <= 21; i++) { //quantidade de nota fixa
                    table.addCell(" ");
                }
            }
            addEmptyRow(table, columns.length);
            addEmptyRow(table, columns.length);
            addEmptyRow(table, columns.length);
            addEmptyRow(table, columns.length);
            addEmptyRow(table, columns.length);
            addEmptyRow(table, columns.length);
            document.add(table);

            document.close();
            return file;
        } catch (Exception ex) {
        }

        return "";
    }

    public void addEmptyRow(PdfPTable table, int columns) {
        for (int i = 0; i < columns; i++) {
            table.addCell(" ");
        }
    }

    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }    
    public Boolean hasPerfomances(Actor actor) {
        try {
            return getPerformanceDAO().hasPerfomances(actor);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        
    }
}
